package com.app.jsonparse;

import java.util.List;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

public class JSONParse {

	private final static String TAG = "JSONPaese";

	public static JSONObject parseObject(String text) throws JSONException {
		JSONObject jsonObject = null;
		jsonObject = JSON.parseObject(text);
		return jsonObject;
	}

	public static long getLongValue(JSONObject jSONObject, String key) {
		try {
			if (jSONObject == null)
				return 0;
			if (jSONObject.containsKey(key)) {
				return jSONObject.getLongValue(key);
			} else {
				return 0;
			}
		} catch (Exception e) {
			return 0;
		}
	}

	public static int getIntValue(JSONObject jSONObject, String key) {
		try {
			if (jSONObject == null)
				return 0;
			if (jSONObject.containsKey(key)) {
				return jSONObject.getIntValue(key);
			} else {
				return 0;
			}
		} catch (Exception e) {
			return 0;
		}
	}

	public static float getFloatValue(JSONObject jSONObject, String key) {
		try {
			if (jSONObject == null)
				return 0.0f;
			if (jSONObject.containsKey(key)) {
				return jSONObject.getFloatValue(key);
			} else {
				return 0.0f;
			}
		} catch (Exception e) {
			return 0.0f;
		}
	}

	public static String getString(JSONObject jSONObject, String key) {
		try {
			if (jSONObject == null)
				return null;
			if (jSONObject.containsKey(key)) {
				return jSONObject.getString(key);
			} else {
				return "";
			}
		} catch (Exception e) {
			return "";
		}
	}

	public static String getString(JSONObject jSONObject, String key,
			String defaultValue) {
		try {
			if (jSONObject == null)
				return defaultValue;
			if (jSONObject.containsKey(key)) {
				return jSONObject.getString(key);
			} else {
				return defaultValue;
			}
		} catch (Exception e) {
			return defaultValue;
		}
	}

	public static JSONObject getJSONObject(JSONObject jSONObject, String key)
			throws JSONException {
		if (jSONObject == null)
			return null;
		if (jSONObject.containsKey(key)) {
			return jSONObject.getJSONObject(key);
		} else {
			return null;
		}
	}

	public static final <T> T parseObject(String text, Class<T> clazz)
			throws JSONException {
		return JSON.parseObject(text, clazz, new Feature[0]);
	}

	public static final <T> List<T> parseArray(String text, Class<T> clazz)
			throws JSONException {
		return JSON.parseArray(text, clazz);
	}

	public static final <T> List<T> parseArray(String text, Class<T> clazz,
			int size) throws JSONException {
		return JSON.parseArray(text, clazz, size);
	}
}
